﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace Helpers
{
    public static class FtpHelper
    {
        private static string UserName = "mkozak-1";

        private static string Password = "z)smmQ6cuQ";

        private static string URL = "ftp://mkozak-1@195.128.121.143/mkozak-2/DB/";

        public static void UploadFile(string filePath)
        {
            System.IO.FileInfo _FileInfo = new System.IO.FileInfo(filePath);

            // Create FtpWebRequest object from the Uri provided

            URL = URL + _FileInfo.Name;

            System.Net.FtpWebRequest _FtpWebRequest = (System.Net.FtpWebRequest)System.Net.FtpWebRequest.Create(new Uri(URL));

            // Provide the WebPermission Credintials

            _FtpWebRequest.Credentials = new System.Net.NetworkCredential(UserName, Password);

            // By default KeepAlive is true, where the control connection is not closed
            // after a command is executed.
            _FtpWebRequest.KeepAlive = false;

            _FtpWebRequest.Timeout = 1000 * 60 * 60 * 3;
            // Specify the command to be executed.
            _FtpWebRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile;
            // Specify the data transfer type.
            _FtpWebRequest.UseBinary = true;
            // Notify the server about the size of the uploaded file
            _FtpWebRequest.ContentLength = _FileInfo.Length;
            // The buffer size is set to 2kb
            int buffLength = 8192;
            byte[] buff = new byte[buffLength];
            // Opens a file stream (System.IO.FileStream) to read the file to be uploaded
            System.IO.FileStream _FileStream = _FileInfo.OpenRead();
            try
            {
                // Stream to which the file to be upload is written
                System.IO.Stream _Stream = _FtpWebRequest.GetRequestStream();
                // Read from the file stream 2kb at a time
                int contentLen = _FileStream.Read(buff, 0, buffLength);

                int size = 0;

                // Till Stream content ends
                while (contentLen != 0)
                {
                    // Write Content from the file stream to the FTP Upload Stream
                    _Stream.Write(buff, 0, contentLen);
                    size = size + contentLen;
                    //Console.WriteLine(string.Format("{0} : {1}", size, _FileStream.Length));
                    contentLen = _FileStream.Read(buff, 0, buffLength);
                }
                // Close the file stream and the Request Stream
                _Stream.Close();
                _Stream.Dispose();
                _FileStream.Close();
                _FileStream.Dispose();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

        }

        public static void DeleteFile(string fileName)
        {
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(URL + fileName);

            request.Credentials = new System.Net.NetworkCredential(UserName, Password);

            request.Timeout = 1000 * 60 * 60 * 3;

            request.KeepAlive = false;

            request.Method = WebRequestMethods.Ftp.DeleteFile;

            FtpWebResponse response = (FtpWebResponse)request.GetResponse();

            response.Close();

        }

    }
}
